$(function(){
    $(function(){
        var params = {};
        var arr = ['id','name','address','phone'];
        $('#add-btn').click(function () {
            $('#myModal').modal('show')
            var row = {}
            var arr = ['id','name','address','phone']
            arr.forEach(function (key) {
                $('#'+key).val(row[key])
            })

        })
        $('#save-btn').click(function () {
            arr.forEach(function (key) {
                params[key] =  $('#'+key).val()

            })
            console.log(params)
            if (params.id) {
                // edit
                fetch('/resume/edit', {
                    method: 'POST',
                    mode: 'cors',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(params)
                }).then(function(response) {
                    location.reload();
                });
            } else {
                // add
                fetch('/resume/add', {
                    method: 'POST',
                    mode: 'cors',
                    credentials: 'include',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(params)
                }).then(function(response) {
                    location.reload();
                });
            }
            $('#myModal').modal('hide')

        })
        $('#table').bootstrapTable({
            url: '/resume/getList',        // 表格数据来源
            pagination: false,
            pageSize: 15,
            pageList: [5, 10, 15, 20],
            showRefresh: false,
            locale: "zh-CN",
            toolbar:'#toolbar',
            columns: [{
                field: 'id',
                title: 'id'
            }, {
                field: 'name',
                title: 'name'
            }, {
                field: 'address',
                title: 'address'
            },{
                field: 'phone',
                title: 'phone'
            }, {
                    field: 'operator',
                    title: 'operator',
                    align: 'center',
                    valign: 'middle',
                    width: '10%',
                    formatter: function (value, row, index) {
                        return '<a href="#editProject" data-toggle="modal" title="modify">' + 'modify' +
                            '<i class="glyphicon glyphicon-pencil" ></i> ' +
                            '</a><a href="javascript:void(0)" title="delete">' + 'delete' +
                            '<i class="glyphicon glyphicon-trash text-danger" ></i> ' +
                            '</a>';
                    },
                    events: {
                        'click a[title=delete]': function (e, value, row, index) {
                            arr.forEach(function (key) {
                                params[key] =  row[key]

                            })
                            $.confirm({
                                title: '确认',
                                content: '确定要删除此人吗?',
                                type: 'green',
                                icon: 'glyphicon glyphicon-question-sign',
                                buttons: {
                                    ok: {
                                        text: '确认',
                                        btnClass: 'btn-primary',
                                        action: function () {
                                            fetch('/resume/delete', {
                                                method: 'POST',
                                                headers: {
                                                    'Content-Type': 'application/json'
                                                },
                                                body: JSON.stringify(params)
                                            }).then(function(response) {
                                                location.reload();
                                            });
                                        }
                                    },
                                    cancel: {
                                        text: '取消',
                                        btnClass: 'btn-primary'
                                    }
                                }
                            });


                        },
                        'click a[title=modify]': function (e, value, row, index) {
                            $('#myModal').modal('show')
                            arr.forEach(function (key) {
                                $('#'+key).val(row[key])
                            })
                            params = row;
                    }
                    }
                }
                ]
        });
    });
});